package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Menu;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.MenuService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.aaa.vegetablemarketexpress.vo.TreeNode;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @FileName: MenuController
 * @Description:
 * @Author: qxh
 * @CreateTime: 2025/9/8 9:45
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("menu")
@Log4j2
public class MenuController extends ResultUtil {
    @Resource
    private MenuService menuService;
    /**
     * @Description: 根据用户id查询菜单
     * @Param: [userId]
     * @return: com.aaa.vegetablemarketexpress.util.Result
     * @Author: qxh
     * @CreateTime: 2025/9/8 9:45
     */
    @GetMapping("queryMenuByUserId")
    public Result queryMenuByUserId(HttpSession  session){
        return ResultUtil.success(menuService.queryMenuByUserId(((User)session.getAttribute("userInfo")).getUserId()));
    }
    /**
     * @Description: 查询所有菜单
     * @Param: []
     * @return: com.aaa.vegetablemarketexpress.util.Result
     * @Author: qxh
     * @CreateTime: 2025/9/8 9:45
     */
    @GetMapping("queryAllMenu")
    public Result queryAllMenu(){
        return ResultUtil.success(menuService.queryAllMenu());
    }

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @PostMapping("/insert")
    public Result<Integer> insert(@RequestBody Menu menu) {
        int result = menuService.insert(menu);
        return ResultUtil.success(result);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @DeleteMapping("/delete/{menuId}")
    public Result<Integer> deleteByPrimaryKey(@PathVariable Long menuId) {
        int result = menuService.deleteByPrimaryKey(menuId);
        return ResultUtil.success(result);
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @PutMapping("/update")
    public Result<Integer> updateByPrimaryKey(@RequestBody Menu menu) {
        int result = menuService.updateByPrimaryKey(menu);
        return ResultUtil.success(result);
    }

    /**
     * 根据菜单 ID 查询菜单
     * @param menuId 菜单 ID
     * @return 菜单信息
     */
    @GetMapping("/select/{menuId}")
    public Result<Menu> selectByPrimaryKey(@PathVariable Long menuId) {
        Menu menu = menuService.selectByPrimaryKey(menuId);
        return ResultUtil.success(menu);
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @PostMapping("/queryByPage")
    public Result queryByPage(@RequestBody Page<Menu> page) {
        PageInfo pageInfo = menuService.queryByPage(page);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Menu>> selectAll() {
        List<Menu> menus = menuService.selectAll();
        return ResultUtil.success(menus);
    }
    /**
     * 根据父菜单 ID 获取子菜单列表
     * @param parentId 父菜单 ID
     * @return 子菜单列表
     */
    @GetMapping("children/{parentId}")
    public Result getChildrenByParentId(@PathVariable Long parentId) {// 用户从 url 路径上获取指定参数，标注在参数前 @PathVariable
        List<Menu> children = menuService.getChildrenByParentId(parentId);
        return ResultUtil.success(children);
    }
    /**
     * 获取指定菜单的子菜单树
     * @return 子菜单树
     */
    @GetMapping("subTree/{parentId}")
    public Result getSubTreeByParentId(@PathVariable Long parentId) {
        List<TreeNode> subTree = menuService.getSubTreeByParentId(parentId);
        return ResultUtil.success(subTree);
    }
}
